#include "DlgMeals.h"
#include <wx/sizer.h>
#include <wx/statbox.h>
#include <wx/stattext.h>
#include <wx/msgdlg.h>
#include <wx/valtext.h>

//(*InternalHeaders(DlgUser)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(DlgUser)
//*)
const wxString CDlgMeals::TABLENAME=_T("meals");
const wxString CDlgMeals::VIEWNAME=_T("VmealsFull");
wxString mealtypeid=_T("10");
#define MORNING _T("上午")
#define AFTERNOON      _T("下午")
#define NIGHT       _T("晚上")
#define TIME_OTHER       _T("其他时间段")
const wxString WINDOWS_TEXT = _T("伙食管理模块");
//控件ID
const long CDlgMeals::IDC_BTN_ADD_MOD= wxNewId();
const long CDlgMeals::IDC_BTN_EXIT= wxNewId();
const long CDlgMeals::IDC_BTN_SEARCH= wxNewId();
const long CDlgMeals::IDC_BTN_DEL= wxNewId();
const long CDlgMeals::IDC_COMBO_TYPE= wxNewId();
const long CDlgMeals::IDC_COMBO_TIME= wxNewId();
const long CDlgMeals::IDC_COMBO_SEARCH= wxNewId();
const long CDlgMeals::IDC_EDIT_SEARCH= wxNewId();
const long CDlgMeals::IDC_EDIT_BALANCE= wxNewId();
const long CDlgMeals::IDC_EDIT_USER= wxNewId();
const long CDlgMeals::IDC_RICHEDIT_REMARK= wxNewId();
const long CDlgMeals::IDC_THEDAY= wxNewId();
const long CDlgMeals::IDC_LIST= wxNewId();
const long CDlgMeals::IDC_LIST_DETAIL= wxNewId();
const long CDlgMeals::IDC_BTN_MENUDEL= wxNewId();
const long CDlgMeals::IDC_COMBO_MENU= wxNewId();
const long CDlgMeals::IDC_BTN_BUY= wxNewId();
const long CDlgMeals::IDC_RICHEDIT_MENU= wxNewId();

BEGIN_EVENT_TABLE(CDlgMeals,wxDialog)
    //(*EventTable(DlgUser)
    //*)
END_EVENT_TABLE()

CDlgMeals::CDlgMeals(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
    :m_addormod_(-1)
{
    //(*Initialize(DlgUser)
    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxMINIMIZE_BOX, _T("id"));
    SetClientSize(wxDefaultSize);
    Move(wxDefaultPosition);
    //*)
    SetTitle(WINDOWS_TEXT);
    SetWindowStyle(GetWindowStyle()|wxRESIZE_BORDER|wxCLOSE_BOX|wxMAXIMIZE_BOX|wxMINIMIZE_BOX);
    CreateCtrlAndEvent();
    EnableAllCtrl(false);
    InitComboBoxAndListCtrl();
}

CDlgMeals::~CDlgMeals()
{
    //(*Destroy(DlgUser)
    //*)
}

void CDlgMeals::CreateCtrlAndEvent()
{
    /////////创建控件并布局
    topSizer_=new wxBoxSizer(wxVERTICAL);//顶层窗口布局,从上到下包含每个了窗口布局
    wxBoxSizer *firSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *secSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *thiSize=new wxBoxSizer(wxHORIZONTAL);
    wxStaticBox *staticBox=new wxStaticBox(this,wxID_ANY,wxT("注意：双击列表数据可进行修改"));
    wxStaticBoxSizer *forSize=new wxStaticBoxSizer(staticBox,wxHORIZONTAL);
    wxBoxSizer *fifSize=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *bottomSize=new wxBoxSizer(wxHORIZONTAL);
    topSizer_->Add(firSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(secSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(thiSize,0,wxEXPAND|wxALL,0);
    topSizer_->Add(forSize,10,wxEXPAND|wxALL,0);
    topSizer_->Add(fifSize,5,wxEXPAND|wxALL,0);
    topSizer_->Add(bottomSize,0,wxEXPAND|wxALL,0);

    combo_search_=new wxChoice(this,IDC_COMBO_SEARCH);
    combo_time_=new wxChoice(this,IDC_COMBO_TIME);
    combo_type_=new wxChoice(this,IDC_COMBO_TYPE);
    btn_Add_Mod_=new wxButton(this,IDC_BTN_ADD_MOD,_T("添加"));
    btn_Del_=new wxButton(this,IDC_BTN_DEL,_T("删除"));
    btn_Exit_=new wxButton(this,IDC_BTN_EXIT,_T("退出"));
    btn_Search_=new wxButton(this,IDC_BTN_SEARCH,_T("查询"));
    text_search_=new wxTextCtrl(this,IDC_EDIT_SEARCH,wxEmptyString);
    text_balance_=new wxNumberCtrl(this,IDC_EDIT_BALANCE,wxEmptyString);
    text_user_=new wxTextCtrl(this,IDC_EDIT_USER,wxEmptyString);
    text_remark_=new wxTextCtrl(this,IDC_RICHEDIT_REMARK,wxEmptyString,wxDefaultPosition,wxSize(120,50),
                                wxTE_PROCESS_ENTER|wxTE_MULTILINE|wxHSCROLL);
    time_=new wxDatePickerCtrl(this,IDC_THEDAY,wxDefaultDateTime,wxDefaultPosition,
                               wxDefaultSize,wxDP_DROPDOWN|wxDP_SHOWCENTURY);
    list_=new wxListCtrl(this,IDC_LIST,wxDefaultPosition,wxDefaultSize,wxLC_REPORT|wxLC_SINGLE_SEL);

    firSize->Add(new wxStaticText(this,wxID_ANY,_T("查询类别")),1, wxTILE|wxALL|wxALIGN_RIGHT,5);
    firSize->Add(combo_search_,1, wxEXPAND|wxALL|wxALIGN_CENTER,5);
    firSize->Add(text_search_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    firSize->Add(btn_Search_,1,wxTILE|wxALL|wxALIGN_LEFT,5);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("时间")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(time_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    secSize->AddStretchSpacer(1);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("时间段")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(combo_time_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    secSize->AddStretchSpacer(1);
    secSize->Add(new wxStaticText(this,wxID_ANY,_T("花费")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    secSize->Add(text_balance_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("经手人")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(text_user_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->AddStretchSpacer(1);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("收支类别")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(combo_type_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    thiSize->AddStretchSpacer(1);
    thiSize->Add(new wxStaticText(this,wxID_ANY,_T("备注")),0,wxFIXED_MINSIZE|wxALL|wxALIGN_RIGHT,5);
    thiSize->Add(text_remark_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    forSize->Add(list_,1,wxEXPAND | wxALL,5);

    btn_buy_=new wxButton(this,IDC_BTN_BUY,_T("购买"));
    btn_menudel_=new wxButton(this,IDC_BTN_MENUDEL,_T("删\r\n除\r\n所\r\n选\r\n菜\r\n肴"));
    list_detail_=new wxListCtrl(this,IDC_LIST_DETAIL,wxDefaultPosition,wxDefaultSize,wxLC_REPORT|wxLC_SINGLE_SEL);
    combo_menu_=new wxChoice(this,IDC_COMBO_MENU);
    text_menu_=new wxTextCtrl(this,IDC_RICHEDIT_MENU,wxEmptyString,wxDefaultPosition,wxSize(120,50),
                              wxTE_PROCESS_ENTER|wxTE_MULTILINE|wxHSCROLL);
    wxBoxSizer *fifSize_left=new wxBoxSizer(wxHORIZONTAL);
    fifSize_left->Add(list_detail_,1,wxEXPAND | wxALL,5);
    wxStaticBoxSizer *fifSize_right=new wxStaticBoxSizer(new wxStaticBox(this,wxID_ANY,wxT("菜肴")),wxVERTICAL);
    wxBoxSizer *fifSize_right_t=new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *fifSize_right_b=new wxBoxSizer(wxHORIZONTAL);
    fifSize_right_t->Add(new wxStaticText(this,wxID_ANY,_T("菜肴名称")),1,wxTILE|wxALL|wxALIGN_RIGHT,5);
    fifSize_right_t->Add(combo_menu_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize_right_t->Add(btn_buy_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize_right_b->Add(text_menu_,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize_right->Add(fifSize_right_t,0,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize_right->Add(fifSize_right_b,1,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize->Add(fifSize_left,5,wxEXPAND|wxALL|wxALIGN_CENTER,5);
    fifSize->Add(btn_menudel_,0,wxTILE|wxALL|wxALIGN_CENTER,5);
    fifSize->Add(fifSize_right,5,wxEXPAND|wxALL|wxALIGN_CENTER,5);

    bottomSize->Add(btn_Add_Mod_,1,wxTILE|wxALL|wxALIGN_RIGHT,5);
    bottomSize->Add(btn_Del_,1,wxTILE|wxALL|wxALIGN_CENTER,5);
    bottomSize->Add(btn_Exit_,1,wxTILE|wxALL|wxALIGN_LEFT,5);

    SetSizer(topSizer_);//绑定对话框和布局控件
    SetAutoLayout(true);
    topSizer_->Fit(this);//调用对话框大小
    topSizer_->SetSizeHints(this);//设置对话框最小大小

    ///////创建事件
    Connect(IDC_BTN_ADD_MOD,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnAddMod);
    Connect(IDC_BTN_EXIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnExit);
    Connect(IDC_BTN_SEARCH,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnSearch);
    Connect(IDC_BTN_DEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnDel);
    Connect(IDC_LIST,wxEVT_COMMAND_LIST_ITEM_SELECTED,(wxObjectEventFunction)&CDlgMeals::OnNMClickList);
    Connect(IDC_LIST,wxEVT_COMMAND_LIST_ITEM_ACTIVATED,(wxObjectEventFunction)&CDlgMeals::OnNMDblclkList);
    Connect(IDC_BTN_BUY,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnBuy);
    Connect(IDC_BTN_MENUDEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&CDlgMeals::OnBnClickedBtnMenudel);
    wxLogDebug(_T("CreateCtrl"));
}

void CDlgMeals::EnableAllCtrl( bool isEnable )
{
    if (m_addormod_==ADDMODEL) {
        FindWindowById(IDC_THEDAY)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TIME)->Enable(isEnable);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(false);
        FindWindowById(IDC_BTN_SEARCH)->Enable(false);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(false);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(false);
        FindWindowById(IDC_BTN_DEL)->Enable(false);
        EnableAllMenuCtrl_(true);
    } else if (m_addormod_==MODMODEL) {
        FindWindowById(IDC_THEDAY)->Enable(false);
        FindWindowById(IDC_COMBO_TIME)->Enable(false);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(false);
        FindWindowById(IDC_BTN_SEARCH)->Enable(false);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(false);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(false);
        FindWindowById(IDC_BTN_DEL)->Enable(false);
        EnableAllMenuCtrl_(true);
    } else {
        FindWindowById(IDC_THEDAY)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TIME)->Enable(isEnable);
        FindWindowById(IDC_EDIT_BALANCE)->Enable(isEnable);
        FindWindowById(IDC_EDIT_USER)->Enable(isEnable);
        FindWindowById(IDC_COMBO_TYPE)->Enable(isEnable);
        FindWindowById(IDC_RICHEDIT_REMARK)->Enable(isEnable);
        FindWindowById(IDC_LIST)->Enable(true);
        FindWindowById(IDC_BTN_SEARCH)->Enable(true);
        FindWindowById(IDC_EDIT_SEARCH)->Enable(true);
        FindWindowById(IDC_COMBO_SEARCH)->Enable(true);
        FindWindowById(IDC_BTN_DEL)->Enable(true);
        EnableAllMenuCtrl_(false);
    }
    wxLogDebug(_T("EnableAllCtrl"));
}

void CDlgMeals::EnableAllMenuCtrl_( bool isEnable )
{
    FindWindowById(IDC_LIST_DETAIL)->Enable(true);
    FindWindowById(IDC_BTN_MENUDEL)->Enable(isEnable);
    FindWindowById(IDC_COMBO_MENU)->Enable(isEnable);
    FindWindowById(IDC_BTN_BUY)->Enable(isEnable);
    FindWindowById(IDC_RICHEDIT_MENU)->Enable(isEnable);
}

void CDlgMeals::InitComboBoxAndListCtrl()
{
    wxChoice *pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_SEARCH),wxChoice);
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    wxListCtrl *pListDetail=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
    pList->SetSingleStyle(wxLC_REPORT);
    pList->SetSingleStyle(wxLC_SINGLE_SEL);
    const vector<pair<wxString,wxString> >&chs_fileds_name=GetFiled_CHS_Name(_T("name_VmealsFull"));
    auto it=chs_fileds_name.cbegin();
    for (; it!=chs_fileds_name.cend(); ++it) {
        wxLogDebug(_T("%s "),it->first);
        pchoice->Append(it->first);
        pList->InsertColumn(pList->GetColumnCount(),it->first,wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);
    }
    const vector<pair<wxString,wxString> >&chs_fileds_name2=GetFiled_CHS_Name(_T("name_VmenuFull"));
    auto it2=chs_fileds_name2.cbegin();
    for (; it2!=chs_fileds_name2.cend(); ++it2) {
        pListDetail->InsertColumn(pListDetail->GetColumnCount(),it2->first,wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);
    }
    pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
    pchoice->Append(MORNING);
    pchoice->Append(AFTERNOON);
    pchoice->Append(NIGHT);
    pchoice->Append(TIME_OTHER);
    pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
    pchoice->Append(_T("二菜一汤"));
    pchoice->Append(_T("一菜一汤"));

    pchoice=wxDynamicCast(FindWindowById(IDC_COMBO_MENU),wxChoice);
    wxString sql=_T("");
    m_menunameandid_=GetMenuName_ID(sql);
    auto nameidit=m_menunameandid_.begin();
    for (; nameidit!=m_menunameandid_.end(); ++nameidit) {
        pchoice->Append(nameidit->first);
    }
    wxLogDebug(_T("InitComboBoxAndListCtrl"));
}

void CDlgMeals::OnBnClickedBtnSearch(wxCommandEvent& event)
{
    wxBusyCursor waticursor;
    wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_SEARCH),wxChoice);
    int index=pBox->GetSelection();
    wxString strComboTextFirst,strComboTextSecond;
    if (index == wxNOT_FOUND) {

    } else {
        strComboTextFirst=pBox->GetString(index);
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    const vector<pair<wxString,wxString> >&chs_filed_name=GetFiled_CHS_Name(_T("name_VmealsFull"));
    if (chs_filed_name.empty()) {
        return;
    }
    vector<pair<wxString,int> >fileds;
    map<wxString,wxString> condition;
    map<wxString,wxString>*pcondit=NULL;
    auto it=chs_filed_name.cbegin();
    for (; it!=chs_filed_name.cend(); ++it) {
        if (strComboTextFirst==it->first) {
            strComboTextSecond=it->second;
        }
        fileds.push_back(std::make_pair(it->second,(int)FILED_TEXT));
    }
    wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_SEARCH),wxTextCtrl);
    wxString csEditText=pText->GetValue();
    csEditText.Trim(true);
    csEditText.Trim(false);
    if (!csEditText.IsEmpty() && !strComboTextFirst.IsEmpty()) {
        condition[strComboTextSecond]=csEditText;
        pcondit=&condition;
    }
    SelFromDb(fileds,VIEWNAME,pcondit,_T("order by theday desc"),pList);
    pList->SetColumnWidth(0,wxLIST_AUTOSIZE);
    wxLogDebug(_T("OnBnClickedBtnSearch"));
}
void CDlgMeals::OnBnClickedBtnDel(wxCommandEvent& event)
{
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    long selitemIndex=-1;
    selitemIndex=pList->GetNextItem(selitemIndex,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);//得到选中的行数
    if (selitemIndex ==-1) {
        return;
    }
    wxString csname=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice)->GetStringSelection();
    wxString tip=_T("确实要删除[")+m_keyvalue_.first+_T(" ")+csname+_T("]的伙食记录吗？");
    if(wxNO==wxMessageBox(tip,_T("删除确认"),wxYES_NO)) {
        return;
    }
    if(!DeleteListDetail_(m_keyvalue_)) {
        return;
    }
    map<wxString,wxString> condition;
    condition[_T("theday")]=m_keyvalue_.first;
    condition[_T("thetime")]=wxString::Format(_T("%d"),m_keyvalue_.second);
    if(DelFromDb(TABLENAME,&condition)) {
        if (!DeleteCash_(m_keyvalue_,mealtypeid)) {
            wxMessageBox(_T("找不到该伙食记录对应的现金流记录（或现对应现金流记录大于1条），")
                         _T("故现金流记录未相应删除，请到现金流管理界面删除相对应现金记录。"),_T("删除未完成"),wxOK);
        } else {
            wxMessageBox(_T("删除完成"),_T("删除完成"),wxOK);
        }
        pList->DeleteItem(selitemIndex);
    } else {
        wxMessageBox(sqlite3_errmsg(g_db),_T("删除出错"));
        return;
    }
    wxLogDebug(_T("OnBnClickedBtnDel"));
}
void CDlgMeals::OnBnClickedBtnExit(wxCommandEvent& event)
{
    if (ADDMODEL==m_addormod_ || MODMODEL==m_addormod_) {
        m_addormod_=-1;
        EnableAllCtrl(false);
        wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
        pbtn->SetLabel(_T("添加"));
        pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
        pbtn->SetLabel(_T("退出"));
        SetLabel(WINDOWS_TEXT);
    } else {
        Close();
    }
    wxLogDebug(_T("OnBnClickedBtnExit"));
}
void CDlgMeals::OnBnClickedBtnAddMod(wxCommandEvent& event)
{
    wxString sql;
    if(ADDMODEL ==m_addormod_ || MODMODEL ==m_addormod_) {
        wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        wxString csTypeName=pText->GetValue();
        if(csTypeName.IsEmpty()) {
            wxMessageBox(_T("花费不能为空"),_T("错误"),wxOK);
            return;
        }
        wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
        csTypeName=pBox->GetStringSelection();
        if(csTypeName.IsEmpty()) {
            wxMessageBox(_T("时间段不能为空"),_T("错误"),wxOK);
            return;
        }
        wxListCtrl *plistdetail=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
        if(plistdetail->GetItemCount()<=0) {
            wxMessageBox(_T("明细菜谱不能为空"),_T("错误"),wxOK);
            return;
        }

        int nIdx;//行索引
        wxDatePickerCtrl *pDT=wxDynamicCast(FindWindowById(IDC_THEDAY),wxDatePickerCtrl);
        wxDateTime time_birthday=pDT->GetValue();
        wxString cashID,username,userID,csfoodname,strremark;
        double dBalance;
        int ntimeslot;
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        pText->GetValue().ToDouble(&dBalance);
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
        username=pText->GetValue();
        pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
        csfoodname=pBox->GetStringSelection();
        pText=wxDynamicCast(FindWindowById(IDC_RICHEDIT_REMARK),wxTextCtrl);
        strremark=pText->GetValue();
        ///////////////////////////////////////////////
        if (csTypeName.CmpNoCase(MORNING)==0) {
            ntimeslot=1;
        } else if (csTypeName.CmpNoCase(AFTERNOON)==0) {
            ntimeslot=2;
        } else if(csTypeName.CmpNoCase(NIGHT)==0) {
            ntimeslot=3;
        } else {
            ntimeslot=4;
        }
        ////由用户名称查找用户ID
        bool isfinduserid=false;
        sql=_T(" where username='")+username+_T("'");
        auto username_id=GetUserName_ID(sql);
        for (auto it=username_id.cbegin(); it!=username_id.cend(); ++it) {
            if (it->first==username) {
                userID=it->second;
                isfinduserid=true;
                break;
            }
        }
        if (isfinduserid==false) {
            wxLogDebug(_T("%s"),sql);
            wxMessageBox(_T("用户名有误,请重新输入"),_T("由用户名称查找用户ID出错"),wxOK);
            return;
        }
        wxListCtrl* pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
        if (ADDMODEL==m_addormod_) { //由添加转为完成
            //添加新记录
            wxString sql=_T("select count(theday) from ")+TABLENAME;
            sql+=_T(" where theDay=@pa1 and theTime=@pa2");
            sqlite3_stmt *stmt=NULL;
            wxString csday=time_birthday.Format(_T("%Y-%m-%d"));
            wxLogDebug(_T("sql=%s,day=%s"),sql,csday);
            if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,0)==SQLITE_OK) {
                int index1 = sqlite3_bind_parameter_index(stmt, "@pa1");
                if(sqlite3_bind_text(stmt, index1, ToDefaultConv(csday), LenDefaultConv(csday), SQLITE_STATIC)!=SQLITE_OK) {
                    wxLogDebug(_T("sqlite3_bind_text on m_keyvalue_.first Error on. %s"), sqlite3_errmsg(g_db));
                }
                index1 = sqlite3_bind_parameter_index(stmt, "@pa2");
                if(sqlite3_bind_int(stmt, index1,ntimeslot)!=SQLITE_OK) {
                    wxLogDebug(_T("sqlite3_bind_text on ntimeslot Error on. %s"), sqlite3_errmsg(g_db));
                }

                if(sqlite3_step(stmt)==SQLITE_ROW) {
                    if(sqlite3_column_text(stmt,0)!=NULL && sqlite3_column_int(stmt,0)!=0) {
                        wxLogDebug(_T("count of %s=%d"),csday,sqlite3_column_int(stmt,0));
                        if (sqlite3_finalize(stmt) != SQLITE_OK) {
                            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                        }
                        wxMessageBox(_T("该时间段的伙食已录入,不允许添加,只允许修改。"),_T("伙食记录重复"),wxOK);
                        return;
                    }
                }
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
            } else {
                wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sql,sqlite3_errmsg(g_db));
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
            }
            map<wxString,wxString> fileds;
            fileds[_T("theday")]=csday;
            fileds[_T("theTime")]=wxString::Format(_T("%d"),ntimeslot);
            fileds[_T("payment")]=wxString::Format(_T("%f"),dBalance);
            fileds[_T("foodname")]=csfoodname;
            fileds[_T("userid")]=userID;
            fileds[_T("remark")]=strremark;
            if(!AddIntoDb(fileds,TABLENAME)) {
                wxMessageBox(sqlite3_errmsg(g_db),_T("添加出错"));
                return;
            }
            auto keyvalue=std::make_pair(csday,ntimeslot);
            if(!SaveListDetail_(keyvalue)) {
                return;
            }
            if (!InsertCash_(keyvalue,dBalance,userID,strremark,mealtypeid)) {
                return;
            }
            m_addormod_=-1;
            EnableAllCtrl(false);
            wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
            pbtn->SetLabel(_T("添加"));
            pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
            pbtn->SetLabel(_T("退出"));
            SetLabel(WINDOWS_TEXT);

            //成功把该行插入到list控件中
            pList->InsertItem(pList->GetItemCount(),_T(""));
            nIdx=pList->GetItemCount()-1;
        } else if (MODMODEL==m_addormod_) { //由修改转为完成
            map<wxString,wxString> fileds,condition;
            condition[_T("theTime")]=wxString::Format(_T("%d"),m_keyvalue_.second);
            condition[_T("theDay")]=m_keyvalue_.first;
            fileds[_T("payment")]=wxString::Format(_T("%f"),dBalance);
            fileds[_T("foodname")]=csfoodname;
            fileds[_T("userid")]=userID;
            fileds[_T("remark")]=strremark;
            if(!ModToDb(fileds,TABLENAME,&condition)) {
                wxMessageBox(sqlite3_errmsg(g_db),_T("修改出错"));
                return;
            }
            if(!SaveListDetail_(m_keyvalue_)) {
                return;
            }
            if (!SaveCash_(m_keyvalue_,dBalance,userID,strremark,mealtypeid)) {
                wxMessageBox(_T("找不到该伙食记录对应的现金流记录（或现对应现金流记录大于1条），")
                             _T("故现金流记录未相应修改，请到现金流管理界面修改相对应现金记录。"),_T("修改未完成"),wxOK);
            }
            m_addormod_=-1;
            EnableAllCtrl(false);
            wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
            pbtn->SetLabel(_T("添加"));
            pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
            pbtn->SetLabel(_T("退出"));
            SetLabel(WINDOWS_TEXT);
            //成功把修改结果反应到list控件中
            nIdx=-1;
            nIdx = pList->GetNextItem(nIdx,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
            if(nIdx == -1) {
                return;
            }
        }
        pList->SetItem(nIdx,0,time_birthday.FormatISODate());
        pList->SetItem(nIdx,1,wxString::Format(_T("%d"),ntimeslot));
        pList->SetItem(nIdx,2,wxString::Format(_T("%f"),dBalance));
        pList->SetItem(nIdx,3,username);
        pList->SetItem(nIdx,4,csfoodname);
        pList->SetItem(nIdx,5,strremark);
    } else {
        m_addormod_=ADDMODEL;
        EnableAllCtrl(true);
        wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
        pbtn->SetLabel(_T("完成"));
        pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
        pbtn->SetLabel(_T("取消添加"));
        SetLabel(WINDOWS_TEXT+_T("_添加"));
        wxTextCtrl *pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);
        pText->SetLabel(_T(""));
        pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
        pText->SetLabel(g_username);
    }
    wxLogDebug(_T("OnBnClickedBtnAddMod"));
}
void CDlgMeals::OnNMClickList(wxListEvent& event)
{
    if(event.GetIndex()==-1) {
        return;
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    m_keyvalue_.first=pList->GetItemText(event.GetIndex(),0);//得到主键值
    wxDateTime bir;
    if(bir.ParseDate(pList->GetItemText(event.GetIndex(),0))) {
        wxDatePickerCtrl *pDT=wxDynamicCast(FindWindowById(IDC_THEDAY),wxDatePickerCtrl);
        pDT->SetValue(bir);
    }
    long ltmp;
    pList->GetItemText(event.GetIndex(),1).ToLong(&ltmp);
    m_keyvalue_.second=ltmp;
    wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TIME),wxChoice);
    pBox->SetSelection(m_keyvalue_.second-1);
    wxTextCtrl* pText=wxDynamicCast(FindWindowById(IDC_EDIT_BALANCE),wxTextCtrl);

    pText->SetValue(pList->GetItemText(event.GetIndex(),2));
    pText=wxDynamicCast(FindWindowById(IDC_EDIT_USER),wxTextCtrl);
    pText->SetValue(pList->GetItemText(event.GetIndex(),3));
    /////////////////////////////////////////////////////////////
    wxString strTmp=pList->GetItemText(event.GetIndex(),4);
    pBox=wxDynamicCast(FindWindowById(IDC_COMBO_TYPE),wxChoice);
    pBox->SetStringSelection(strTmp);
////////////////////////////////////////////////////////////////
    pText=wxDynamicCast(FindWindowById(IDC_RICHEDIT_REMARK),wxTextCtrl);
    pText->SetValue(pList->GetItemText(event.GetIndex(),5));
    InsertListDetail_(m_keyvalue_);
    wxLogDebug(_T("OnNMClickList"));
}
void CDlgMeals::OnNMDblclkList(wxListEvent& event)
{
    if(event.GetIndex()==-1) {
        return;
    }
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST),wxListCtrl);
    m_keyvalue_.first=pList->GetItemText(event.GetIndex(),0);//得到主键值
    long ltmp;
    pList->GetItemText(event.GetIndex(),1).ToLong(&ltmp);
    m_keyvalue_.second=ltmp;
    m_addormod_=MODMODEL;
    EnableAllCtrl(true);
    wxButton *pbtn=wxDynamicCast(FindWindowById(IDC_BTN_ADD_MOD),wxButton);
    pbtn->SetLabel(_T("保存"));//修改完回到待增加状态
    pbtn=wxDynamicCast(FindWindowById(IDC_BTN_EXIT),wxButton);
    pbtn->SetLabel(_T("取消修改"));
    wxString text=WINDOWS_TEXT+_T("_修改");
    SetLabel(text);
    wxLogDebug(_T("OnNMDblclkList"));
}
void CDlgMeals::OnBnClickedBtnBuy(wxCommandEvent& event)
{
    wxString csTemp;
    wxChoice *pBox=wxDynamicCast(FindWindowById(IDC_COMBO_MENU),wxChoice);
    csTemp=pBox->GetStringSelection();
    if(csTemp.IsEmpty()) {
        return;
    }
    wxString menuName=csTemp;
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
    for (int i=0; i<pList->GetItemCount(); ++i) {
        wxString itemtext=pList->GetItemText(i,1);
        if (itemtext.CmpNoCase(csTemp)==0) {
            wxMessageBox(_T("该菜谱已存在，不能购买，请选择其他菜谱。"),_T("菜谱重复"),wxOK);
            return;
        }
    }
    const vector<pair<wxString,wxString> >&chs_fileds_name=GetFiled_CHS_Name(_T("name_VmenuFull"));
    auto it=chs_fileds_name.begin();
    wxString sql=_T("select ");
    for (; it!=chs_fileds_name.end(); ++it) {
        sql+=it->second+_T(",");
    }
    sql=sql.substr(0,sql.length()-1);
    sql+=_T(" from VmenuFull where menuname='")+menuName+_T("'");
    sqlite3_stmt *stmt=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,NULL)==SQLITE_OK) {
        if(sqlite3_step(stmt)==SQLITE_ROW) {
            int row_count=pList->GetItemCount();
            pList->InsertItem(row_count,_T(""));
            pList->SetItem(row_count,0, wxString((const char*)sqlite3_column_text(stmt,0), DefaultConv));
            pList->SetItem(row_count,1, wxString((const char*)sqlite3_column_text(stmt,1), DefaultConv));
            pList->SetItem(row_count,2, wxString((const char*)sqlite3_column_text(stmt,2), DefaultConv));
            pList->SetItem(row_count,3, wxString((const char*)sqlite3_column_text(stmt,3), DefaultConv));
            pList->SetItem(row_count,4, wxString((const char*)sqlite3_column_text(stmt,4), DefaultConv));
        }
        if (sqlite3_finalize(stmt) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
    } else {
        wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sql,sqlite3_errmsg(g_db));
        if (sqlite3_finalize(stmt) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
        return;
    }
    wxLogDebug(_T("OnBnClickedBtnBuy"));
}
void CDlgMeals::OnBnClickedBtnMenudel(wxCommandEvent& event)
{
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
    long selitemIndex=-1;
    selitemIndex=pList->GetNextItem(selitemIndex,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);//得到选中的行数
    if (selitemIndex ==-1) {
        return;
    }
    wxString csid=pList->GetItemText(selitemIndex,0);
    wxString csname=pList->GetItemText(selitemIndex,1);
    wxString tip=_T("确实要删除编号为[")+csid+_T("]，菜名为[")+csname+_T("]的菜谱吗？");
    if(wxNO==wxMessageBox(tip,_T("删除确认"),wxYES_NO)) {
        return;
    }
    pList->DeleteItem(selitemIndex);
    wxLogDebug(_T("OnBnClickedBtnMenudel"));
}

bool CDlgMeals::SaveListDetail_(pair<wxString,int>& keyvalue)//修改或增加LIST_DETAIL控件里的记录
{
    wxString sql;
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
    if (m_addormod_==MODMODEL) {
        map<wxString,wxString> condition;
        condition[_T("theDay")]=keyvalue.first;
        condition[_T("theTime")]=wxString::Format(_T("%d"),keyvalue.second);
        if(!DelFromDb(_T("cuisine"),&condition)) {
            wxMessageBox(_T("删除明细数据出错"),_T("出错"),wxOK);
            return false;
        }
    }

    sql=_T("insert into cuisine (theDay,theTime,menuID) ");
    sql+=_T(" values(@pa1,@pa2,@pa3)");
    sqlite3_stmt *stmt=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,NULL)==SQLITE_OK) {
        sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);//开始事务
        for (int i=0; i<pList->GetItemCount(); ++i)	{
            int index1 = sqlite3_bind_parameter_index(stmt, "@pa1");
            if(sqlite3_bind_text(stmt, index1, ToDefaultConv(keyvalue.first), LenDefaultConv(keyvalue.first), SQLITE_STATIC)!=SQLITE_OK) {
                wxLogDebug(_T("sqlite3_bind_text on keyvalue.first Error on. %s"), sqlite3_errmsg(g_db));
            }
            index1 = sqlite3_bind_parameter_index(stmt, "@pa2");
            if(sqlite3_bind_int(stmt, index1,keyvalue.second)!=SQLITE_OK) {
                wxLogDebug(_T("sqlite3_bind_text on keyvalue.second Error on. %s"), sqlite3_errmsg(g_db));
            }
            index1 = sqlite3_bind_parameter_index(stmt, "@pa3");
            wxString csmenuID=pList->GetItemText(i,0);
            if(sqlite3_bind_text(stmt, index1, ToDefaultConv(csmenuID), LenDefaultConv(csmenuID), SQLITE_STATIC)!=SQLITE_OK) {
                wxLogDebug(_T("sqlite3_bind_text on csmenuID Error on. %s"), sqlite3_errmsg(g_db));
            }
            sqlite3_step(stmt);//commit
            sqlite3_reset(stmt); //clean
        }
        sqlite3_exec(g_db, "END TRANSACTION", NULL, NULL, NULL);//结束事务
        if (sqlite3_finalize(stmt) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
    } else {
        wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sql,sqlite3_errmsg(g_db));
        if (sqlite3_finalize(stmt) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
        return false;
    }
    return true;
}
bool CDlgMeals::InsertListDetail_(pair<wxString,int>& keyvalue)//将记录添加到LIST_DETAIL控件里
{
    wxListCtrl *pList=wxDynamicCast(FindWindowById(IDC_LIST_DETAIL),wxListCtrl);
    pList->DeleteAllItems();
    wxString sqlMenuID;
    sqlMenuID=_T("select menuID from cuisine where theDay=@pa1 and theTime=@pa2");
    const vector<pair<wxString,wxString> >&chs_fileds_name=GetFiled_CHS_Name(_T("name_VmenuFull"));
    auto it=chs_fileds_name.cbegin();
    wxString sqlMenuAll=_T("select ");
    for (; it!=chs_fileds_name.cend(); ++it) {
        sqlMenuAll+=it->second+",";
    }
    sqlMenuAll=sqlMenuAll.substr(0,sqlMenuAll.length()-1);
    sqlMenuAll+=_T(" from VmenuFull where menuID=@pa1");
    sqlite3_stmt *conSelMenuID=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sqlMenuID),LenDefaultConv(sqlMenuID),&conSelMenuID,NULL)!=SQLITE_OK) {
        wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sqlMenuID,sqlite3_errmsg(g_db));
        if (sqlite3_finalize(conSelMenuID) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
        return false;
    }
    int index1 = sqlite3_bind_parameter_index(conSelMenuID, "@pa1");
    if(sqlite3_bind_text(conSelMenuID, index1, ToDefaultConv(keyvalue.first), LenDefaultConv(keyvalue.first), SQLITE_STATIC)!=SQLITE_OK) {
        wxLogDebug(_T("sqlite3_bind_text on keyvalue.first Error on. %s"), sqlite3_errmsg(g_db));
    }
    index1 = sqlite3_bind_parameter_index(conSelMenuID, "@pa2");
    if(sqlite3_bind_int(conSelMenuID, index1,keyvalue.second)!=SQLITE_OK) {
        wxLogDebug(_T("sqlite3_bind_text on keyvalue.second Error on. %s"), sqlite3_errmsg(g_db));
    }

    sqlite3_stmt *conSelMenuAll=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sqlMenuAll),LenDefaultConv(sqlMenuAll),&conSelMenuAll,NULL)!=SQLITE_OK) {
        wxLogDebug(_T("%s 语法有误. 错误信息:%s"), sqlMenuAll,sqlite3_errmsg(g_db));
        if (sqlite3_finalize(conSelMenuID) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
        if (sqlite3_finalize(conSelMenuAll) != SQLITE_OK) {
            wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
        }
        return false;
    }
    sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);//开始事务

    while(sqlite3_step(conSelMenuID)==SQLITE_ROW) {
        wxString menuid((const char*)sqlite3_column_text(conSelMenuID,0),DefaultConv);
        index1 = sqlite3_bind_parameter_index(conSelMenuAll, "@pa1");
        if(sqlite3_bind_text(conSelMenuAll, index1, ToDefaultConv(menuid), LenDefaultConv(menuid), SQLITE_STATIC)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on menuid Error on. %s"), sqlite3_errmsg(g_db));
        }
        while(sqlite3_step(conSelMenuAll)==SQLITE_ROW) {
            int row_count=pList->GetItemCount();
            pList->InsertItem(row_count, _T(""));
            wxString menuid((const char*)sqlite3_column_text(conSelMenuAll,0),DefaultConv);
            wxString menuname((const char*)sqlite3_column_text(conSelMenuAll,1),DefaultConv);
            wxString material((const char*)sqlite3_column_text(conSelMenuAll,2),DefaultConv);
            wxString menutypename((const char*)sqlite3_column_text(conSelMenuAll,3),DefaultConv);
            wxString strremark((const char*)sqlite3_column_text(conSelMenuAll,4),DefaultConv);
            pList->SetItem(row_count,0, menuid);
            pList->SetItem(row_count,1, menuname);
            pList->SetItem(row_count,2, material);
            pList->SetItem(row_count,3, menutypename);
            pList->SetItem(row_count,4, strremark);
        }
        sqlite3_reset(conSelMenuAll);
    }
    sqlite3_exec(g_db, "END TRANSACTION", NULL, NULL, NULL);//结束事务
    //释放stmt
    if (sqlite3_finalize(conSelMenuID) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    if (sqlite3_finalize(conSelMenuAll) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    return true;
}
bool CDlgMeals::DeleteListDetail_(pair<wxString,int>& keyvalue)   //删除数据库中的明细记录
{
    map<wxString,wxString> condition;
    condition[_T("theday")]=keyvalue.first;
    condition[_T("thetime")]=wxString::Format(_T("%d"),keyvalue.second);
    if(!DelFromDb(_T("cuisine"),&condition)) {
        wxMessageBox(sqlite3_errmsg(g_db),_T("删除明细数据出错"));
        return false;
    }
    return true;
}
bool CDlgMeals::SaveCash_(pair<wxString,int>& keyvalue,double &dbalance,
                          wxString &userID,wxString &remark, wxString &typeID)   //根据修改的记录相应修改现金流表的记录
{
    wxString sqlTmp;
    //寻找该时段的伙食费(编号10)
    sqlTmp=_T("select count(cashID) from cash where date(theDay)=date(@pa1) and theTime=@pa2 and typeID=")+typeID;
    sqlite3_stmt *stmt=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sqlTmp),LenDefaultConv(sqlTmp),&stmt,0)==SQLITE_OK) {
        int index1 = sqlite3_bind_parameter_index(stmt, "@pa1");
        if(sqlite3_bind_text(stmt, index1, ToDefaultConv(keyvalue.first), LenDefaultConv(keyvalue.first), SQLITE_STATIC)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on m_keyvalue_.first Error on. %s"), sqlite3_errmsg(g_db));
        }
        index1 = sqlite3_bind_parameter_index(stmt, "@pa2");
        if(sqlite3_bind_int(stmt, index1,keyvalue.second)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on ntimeslot Error on. %s"), sqlite3_errmsg(g_db));
        }
        if(sqlite3_step(stmt)==SQLITE_ROW) {
            if(sqlite3_column_text(stmt,0)==NULL || sqlite3_column_int(stmt,0)!=1) {
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
                return false;//找不到惟一记录
            }
        }
    }
    if (sqlite3_finalize(stmt) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    //修改该记录
    map<wxString,wxString> fileds,condition;
    condition[_T("theTime")]=wxString::Format(_T("%d"),keyvalue.second);
    condition[_T("theDay")]=keyvalue.first;
    condition[_T("typeID")]=typeID;
    fileds[_T("balance")]=wxString::Format(_T("%f"),dbalance*-1);
    fileds[_T("userid")]=userID;
    fileds[_T("remark")]=remark;
    if(!ModToDb(fileds,_T("cash"),&condition)) {
        wxMessageBox(sqlite3_errmsg(g_db),_T("修改出错"));
        return false;
    }
    return true;
}
bool CDlgMeals::InsertCash_(pair<wxString,int>& keyvalue,double &dbalance,
                            wxString &userID,wxString &remark, wxString &typeID)   //根据修改的记录添加到现金流表中
{
    wxString sqlTmp;
    wxString cashID;
    wxString csday=keyvalue.first;
    csday.Replace(wxString(_T("-")),wxEmptyString,true);
    //寻找该时段的伙食费(编号10)
    sqlTmp=_T("select max(cashID) from cash  where cashID like '")+csday+_T("%'");
    sqlite3_stmt *stmt=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sqlTmp),LenDefaultConv(sqlTmp),&stmt,0)==SQLITE_OK) {
        if(sqlite3_step(stmt)==SQLITE_ROW) {
            if(sqlite3_column_text(stmt,0)==NULL) {//当天第一条记录
                cashID=csday+_T("0001");
            } else {
                wxString maxid=wxString((const char*)sqlite3_column_text(stmt,0),DefaultConv);
                cashID=maxid.substr(8,maxid.length());//得到最后四位序号
                long nid;
                cashID.ToLong(&nid);
                ++nid;
                cashID=wxString::Format(_T("%s%04d"),csday,nid);
            }
        }
    }
    if (sqlite3_finalize(stmt) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    //修改该记录
    map<wxString,wxString> fileds;
    fileds[_T("cashID")]=cashID;
    fileds[_T("theTime")]=wxString::Format(_T("%d"),keyvalue.second);
    fileds[_T("theDay")]=keyvalue.first;
    fileds[_T("typeID")]=typeID;
    fileds[_T("balance")]=wxString::Format(_T("%f"),dbalance*-1);
    fileds[_T("userid")]=userID;
    fileds[_T("remark")]=remark;
    if(!AddIntoDb(fileds,_T("cash"))) {
        wxMessageBox(sqlite3_errmsg(g_db),_T("修改出错"));
        return false;
    }
    return true;
}
bool CDlgMeals::DeleteCash_(pair<wxString,int>& keyvalue, wxString &typeID)   //根据修改的记录删除现金流表中的记录
{
    wxString sql;
    //寻找该时段的伙食费(编号10)
    sql=_T("select count(cashID) from cash where theDay=@pa1 and theTime=@pa2 and typeID=")+typeID;
    sqlite3_stmt *stmt=NULL;
    if(sqlite3_prepare_v2(g_db,ToDefaultConv(sql),LenDefaultConv(sql),&stmt,0)==SQLITE_OK) {
        int index1 = sqlite3_bind_parameter_index(stmt, "@pa1");
        if(sqlite3_bind_text(stmt, index1, ToDefaultConv(keyvalue.first), LenDefaultConv(keyvalue.first), SQLITE_STATIC)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on m_keyvalue_.first Error on. %s"), sqlite3_errmsg(g_db));
        }
        index1 = sqlite3_bind_parameter_index(stmt, "@pa2");
        if(sqlite3_bind_int(stmt, index1,keyvalue.second)!=SQLITE_OK) {
            wxLogDebug(_T("sqlite3_bind_text on ntimeslot Error on. %s"), sqlite3_errmsg(g_db));
        }
        if(sqlite3_step(stmt)==SQLITE_ROW) {
            if(sqlite3_column_text(stmt,0)==NULL || sqlite3_column_int(stmt,0)!=1) {
                if (sqlite3_finalize(stmt) != SQLITE_OK) {
                    wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
                }
                return false;//找不到惟一记录
            }
        }
    }
    if (sqlite3_finalize(stmt) != SQLITE_OK) {
        wxLogDebug(_T("Error on sqlite3_finalize: %s"), sqlite3_errmsg(g_db));
    }
    //修改该记录
    map<wxString,wxString> fileds,condition;
    condition[_T("theTime")]=wxString::Format(_T("%d"),keyvalue.second);
    condition[_T("theDay")]=keyvalue.first;
    condition[_T("typeID")]=typeID;
    if(!DelFromDb(_T("cash"),&condition)) {
        wxMessageBox(sqlite3_errmsg(g_db),_T("删除出错"));
        return false;
    }
    return true;
}
